﻿using AutoMapper;
using FlyingHat.Core.Entities;
using FlyingHat.Model.Models;
using FlyingHat.Model.Models.Global;
using FlyingHat.Repository.IRepository;
using FlyingHat.Service.IService;

namespace FlyingHat.Service.Service
{
    public class ClassificationService : IClassificationService
    {
        private readonly IClassificationRepository classificationRepository;
        private readonly IMapper mapper;

        public ClassificationService(IClassificationRepository classificationRepository, IMapper mapper)
        {
            this.classificationRepository = classificationRepository;
            this.mapper = mapper;
        }

        public async Task<IReturnModel> AddOrUpdate(ClassificationModel model)
        {
            var cls = mapper.Map<Classification>(model);
            var flag = false;
            var id = 0L;
            if (model.Id > 0)
            {
                id = model.Id;
                flag = await classificationRepository.UpdateAsync(cls, i => new { i.Code, i.Name, i.Sort }).ConfigureAwait(false);
            }
            else
            {
                id = await classificationRepository.AddReturnIdAsync(cls);
            }
            return ReturnModel.Result(flag, id, "保存");
        }


        public async Task<IReturnModel> GetTree(long spreadId)
        {
            var list = await classificationRepository.GetListAsync().ConfigureAwait(false);
            var tree = GetTree(list, spreadId);
            return ReturnModel.Success("", new List<ClassificationTreeModel>() { tree });
        }

        static ClassificationTreeModel GetTree(List<Classification> list, long spreadId)
        {
            var root = new ClassificationTreeModel()
            {
                Id = 0,
                Title = "中图分类",
                ParentId = -1,
                Children = list.Where(i => i.ParentId == 0).OrderBy(i => i.Sort).Select(i => new ClassificationTreeModel
                {
                    Id = i.Id,
                    Code = i.Code,
                    Name = i.Name,
                    ParentName = "中图分类",
                    Sort = i.Sort,
                    Title = $"{i.Code} {i.Name}",
                    ParentId = i.ParentId,
                    SpreadId = spreadId
                }).ToList()
            };
            list = list.Where(i => i.ParentId != 0).ToList();
            var current = new List<ClassificationTreeModel>();
            current = root.Children;
            // 获取当前级入栈
            Stack<ClassificationTreeModel> stack = new(current);
            while (stack.Count > 0)
            {
                var model = stack.Pop();
                model.Children = list.Where(i => i.ParentId == model.Id).OrderBy(i => i.Sort).Select(i => new ClassificationTreeModel
                {
                    Id = i.Id,
                    Code = i.Code,
                    Name = i.Name,
                    Sort = i.Sort,
                    Title = $"{i.Code} {i.Name}",
                    ParentId = i.ParentId,
                    ParentName = model.Name,
                    SpreadId = spreadId
                }).ToList();
                if (!model.Last)
                {
                    foreach (var child in model.Children)
                    {
                        stack.Push(child);
                    }
                }
            }
            return root;
        }

        public async Task<IReturnModel> Delete(long id)
        {
            // 此处后期需要检测是否已经绑定图书室
            // 1.查询当前分类及其所有的子类


            throw new ArgumentNullException();
        }

    }
}
